package com.appcenter.application.controller;

import com.appcenter.application.input.App.CreateAppInput;
import com.appcenter.application.input.User.*;
import com.appcenter.application.service.UserService;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/services/jzappcenterservice/user")
@Api("用户相关服务操作")
public class UserServiceController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "获取单个用户", notes = "获取单个用户", produces = "application/json")
    @PostMapping("/GetUser")
    public ResponseEntity<JsonResult> GetUser(@RequestBody GetUserInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(userService.GetUser(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表", produces = "application/json")
    @PostMapping("/GetUsers")
    public ResponseEntity<JsonResult> GetUsers(@RequestBody GetUsersInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(userService.GetUsers(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }
    @ApiOperation(value = "重置密码", notes = "重置密码", produces = "application/json")
    @PostMapping("/ChangePassword")
    public ResponseEntity<JsonResult> ChangePassword(@RequestBody ChangePasswordInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(userService.ChangePassword(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }
    @ApiOperation(value = "修改密码", notes = "修改密码", produces = "application/json")
    @PostMapping("/UpdatePassword")
    public ResponseEntity<JsonResult> UpdatePassword(@RequestBody UpdatePasswordInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(userService.UpdatePassword(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取多个用户", notes = "获取多个用户", produces = "application/json")
    @PostMapping("/GetUserInfos")
    public ResponseEntity<JsonResult> GetUserInfos(@RequestBody GetUserInfosInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(userService.GetUserInfos(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }
    @ApiOperation(value = "查询用户", notes = "查询用户", produces = "application/json")
    @PostMapping("/SearchUsers")
    public ResponseEntity<JsonResult> SearchUsers(@RequestBody SearchUsersInput input) throws Exception {
        //throw new ErrorJsonResult("1000001", "捕获异常信息");
        JsonResult r = new JsonResult();
        try {
            r.setResult(userService.SearchUsers(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }

        return ResponseEntity.ok(r);
    }
}
